/*
 *
 * Created on: 22.09.2012
 *
 * config.h
 * This file is part of KOBUS
 * 
 * Copyright (C) 2012 - Ivan Penkin, MIPT
 * grek.penkin@gmail.com
 *
 * KOBUS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * KOBUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KOBUS; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 *  
 */

#ifndef CONFIG_H_
#define CONFIG_H_

#include <KobusCommon_global.h>
#include <exception.h>
#include <qobject.h>


namespace kobus
{

/*
 *
 * Class Config is a singletone.
 * For creation a copy, you should use
 * non-default constructor. You can't
 * create two or more copies of Config.
 * You can use default constructor to
 * have the Config instance.
 *
 */
class ConfigPrivate;

/*
 *
 * uses Singleton pattern
 *
 */
class KOBUS_COMMON_EXPORT Config: public QObject
{
    Q_OBJECT

  public:
    class Key
    {
      public:
        virtual
        const QString sectionName() const = 0;
        virtual
        const QString getName() const = 0;
    };

    explicit
    Config(QObject * parent = 0);
    Config(const QString & /* cfgPath */, QObject * parent = 0);

    virtual
    ~Config();

    void read() throw (Exception);

    bool           contains(const Key & /* key */) const;
    const QString getString(const Key & /* key */) const throw (Exception);
    int           getInt(const Key & /* key */)    const throw (Exception);
    QList<QString>     keys() const;

    virtual
    bool check() const = 0;

  protected:
    Config(ConfigPrivate * dd, QObject * parent);

    static ConfigPrivate * d_ptr;

  private:
    Q_DISABLE_COPY(Config);
    Q_DECLARE_PRIVATE(Config);
};

} /* namespace kobus */

Q_DECLARE_TYPEINFO(kobus::Config, Q_MOVABLE_TYPE);

#define K_CONFIG_KEYS_BEGIN \
  class Keys \
  { \
    public:

#define K_CONFIG_KEYS_END \
  };

#define K_CONFIG_SECTION_BEGIN(cppName,cfgName) \
  class cppName \
  { \
      class Key__: public kobus::Config::Key \
      { \
        public: \
          const QString sectionName() const { \
                  cppName s; \
                  return s.getSection(); \
                }; \
      }; \
    public: \
      const QString getSection() {return #cfgName;};

#define K_CONFIG_SECTION_END \
  };

#define K_CONFIG_DECLARE_KEY(cppName,cfgName) \
    private: \
      class cppName##__: public Key__ \
      { \
        public: \
          const QString getName() const { \
                  return #cfgName; \
                }; \
      }; \
    public: \
      static const cppName##__ cppName;

#define INIT_CONFIG_KEY(Key) \
    const Key##__ Key;


#endif /* CONFIG_H_ */
